﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite;
using System.Media;

namespace Stone
{
    public class Module_Radio
    {
        //Variables
        public string RDSname = "";
        public string RDSmessage = "";
        private int CurrentFrequency_ = 0;
        private DB db = new DB(Settings.DB_Settings_Path);
        public Station[] stations = new Station[] { };

        //Properties
        public int CurrentFrequency
        {
            get
            {
                return CurrentFrequency_;
            }
            set
            {
                CurrentFrequency_ = value;
                db.Connect();
                string query = "UPDATE tuner_settings SET value = '" + value + "' WHERE name = 'lastFrequency'";
                db.ExecuteNonQuery(query);

            }
        }

        //Methods
        public string GetCurrentFrequencyFormatted()
        {
            string frequency_str = CurrentFrequency.ToString();

            if (frequency_str.Length >= 4)
            {
                string frequencyLeft = frequency_str.Substring(0, frequency_str.Length - 2);
                string frequencyRight = frequency_str.Substring(frequency_str.Length - 2, 2);
                return frequencyLeft + "." + frequencyRight;
            }
            else
                return "0.00";
        }
        public void Station_Add(Station witch)
        {
            Station[] tmpArr = new Station[stations.Length + 1];
            stations.CopyTo(tmpArr, 0);
            tmpArr.SetValue(witch, stations.Length);
            stations = tmpArr;
        }
        public Station Station_get(int id)
        {
            Station result = null;

            foreach (Station tmp in stations)
            {
                if (tmp.id == id)
                {
                    result = tmp;
                    break;
                }
            }

            return result;
        }
        public Station[] GetStations()
        {
            return stations;
        }
        public void ON() { }
        public void OFF() { }
        public void Tune(int frequency) { }
        public void Tune(string memoryNumber) { }
        public void Mute(bool yes) { }
        public void SeekUp() { }
        public void SeekDown() { }
        public void TuneFrequencyUp(int step) { }
        public void TuneFrequencyDown(int step) { }
        public void StoreStation(int id, string name, int frequency) { }
        public void StoreStation(string id, string name, int frequency) 
        {
            db.Connect();
            string query = "UPDATE tuner_favourites SET frequency = '" + frequency + "', name = '" + name + "' WHERE id = '" + id + "'";
            db.ExecuteNonQuery(query);

            SoundPlayer simpleSound = new SoundPlayer(Settings.Audio_Path + "\\beep.wav");
            simpleSound.Play();
            LoadStations();
        }
        public void LoadStations()
        {
            db.Connect();
            DataTable dt2 = db.ExecuteQuery("SELECT * FROM tuner_settings");

            foreach (DataRow tmp in dt2.Rows)
            {
                if(tmp["name"].ToString() == "lastFrequency")
                    CurrentFrequency_ = Convert.ToInt32(tmp["value"].ToString());
            }

            DataTable dt = db.ExecuteQuery("SELECT * FROM tuner_favourites");

            foreach (DataRow tmp in dt.Rows)
            {
                Station newStation = new Station();
                newStation.id = Convert.ToInt32(tmp["id"].ToString());
                newStation.name = tmp["name"].ToString();
                newStation.frequency = Convert.ToInt32(tmp["frequency"].ToString());
                newStation.InUse = Convert.ToInt32(tmp["InUse"].ToString());
                Station_Add(newStation);
            }
        }
        public void TuneStoredStation(int memoryNumber) { }
        public int SignalStrenght() { return -1; }
    }

    public class Station
    {
        public int id = -1;
        public string name = "n/a";
        public int frequency = -1;
        public int InUse = 0;
    }
}
